package com.nhy.bloggers.interceptor;

import com.nhy.bloggers.utils.JWTUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从header中获得token
        String jwtToken = request.getHeader("user_token");
        Claims claims = JWTUtils.parseJWT(jwtToken);
        if(claims!=null){
            return true;
        }

        response.setStatus(401);
        return true;
    }
}
